<?php

/**
 * CategoryService.class.php 
 * 
 *
 * @author     late.xiao@qq.com
 */
namespace Sysblog\Service;

class CategoryService extends BaseService {
    
    public function saveData($request)
    {
        if(!$request)
        {
            return false;
        }
        
        $node_id = null;
        if(isset($request['node_id']))
        {
            $node_id = rintval($request['node_id']);
        }
        
        // 验证数据
        $this->__checkData($request);
        $obj_mdl = D('article_nodes');
        
        if($node_id)
        {
            $where['node_id'] = $node_id;
            $data['updated'] = NOW_TIME;
            $data['parent_id'] = $request['parent_id'];
            $data['order_sort'] = $request['order_sort'];
            $data['node_name'] = $request['node_name'];
            
            return $obj_mdl->where($where)->save($data);
        }
        
        $data['created'] = $data['updated'] = NOW_TIME;
        $data['parent_id'] = $request['parent_id'];
        $data['order_sort'] = $request['order_sort'];
        $data['node_name'] = $request['node_name'];
        
        return $obj_mdl->data($data)->add();
    }
    /**
     * 获取分类列表
     *
     * @param string $field
     * @param bool $isParent true只取一级分类，false取所有的分类
     * @return array
     * */
    public function getCatList($field="*", $filter = array(), $isParent=false)
    {
        $data = array();
        $obj_mdl = D('article_nodes');
        if($isParent)
        {
            $obj_mdl->where('parent_id=%d', 0);
        }
        if($filter && is_array($filter))
        {
            $obj_mdl->where($filter);
        }
    
        $data = $obj_mdl->field($field)->select();
    
        return $data;
    }
    
    // 根据主键获取一条数据
    public function getCatRow($cid, $field='*')
    {
        $data = array();
        $obj_mdl = D('article_nodes');
        $data = $obj_mdl->where('node_id=%d', intval($cid))->field($field)->find();
        if(!$data)
        {
            return false;
        }
    
        return $data;
    }
    
    public function delRow($cid)
    {
        $cid = intval($cid);
        if(!$cid) return false;
        $obj_mdl = D('article_nodes');
        // 查看此分类下是否有子类
        $filter['parent_id'] = $cid;
        $rs = $obj_mdl->where($filter)->select();
        if($rs)
        {
            throw new \Exception(L('_ERR_CAT_DEL'));
            return false;
        }
        
        $where['node_id'] = $cid;
        return $obj_mdl->where($where)->delete();
    }
    
    // 格式化数据
    public function proCatList($data)
    {
        if(!is_array($data) || !$data)
        {
            return false;
        }
    
        // 获取所有的父类
        $pcatList = array();
        foreach ($data as $v)
        {
            if($v['parent_id'] == 0)
            {
                $pcatList[$v['node_id']] = $v;
            }
        }
        // 组合数据
        foreach ($data as $v)
        {
            if(array_key_exists($v['parent_id'], $pcatList))
            {
                $pcatList[$v['parent_id']]['child'][] = $v;
            }
        }
    
        return $pcatList;
    }
    
    private function __checkData($data)
    {
        $errors = array();
        $options = array(
                'node_name' =>array(
                        'required' => array(
                                'rule'=>true,
                                'message'=>'分类名称必填'
                        ),
                        'string_count_in_range' => array(
                                'rule'=>array_values(array('min'=>1, 'max'=>8)),
                                'message'=>'分类名称最多8个字'
                        ),
        
                ),
        );
        
        $rs = \Base\Lib\Tools\Validator::make($errors, $data, $options);
        if(!$rs)
        {
            foreach ($errors as $k=>$v)
            {
                throw new \Exception($v);
            }
        }
        
        if($data['order_sort'])
        {
            if(!$rs = is_numeric($data['order_sort']))
            {
                throw new \Exception('排序必须是数字');
            }
        }
        
        if($data['parent_id'])
        {
            if(!$rs = is_numeric($data['parent_id']))
            {
                throw new \Exception('上级分类设置错误');
            }
        }
        
        return $rs;
    }

}
 